import { defineStore } from "pinia";
import { Message } from "@arco-design/web-vue";
import * as merchantBaseApi from "@/api/merchant-base";

export const useMerchantBaseStore = defineStore("merchantBase", () => {
  async function getMerchants(param?: any) {
    const response = await merchantBaseApi.getMerchants(param);
    if (response.code === 20000) {
      return response.data.merchants;
    }
    return [];
  }

  async function getMerchant(param?: any) {
    const response = await merchantBaseApi.getMerchant(param);
    if (response.code === 20000) {
      return response.data.merchant;
    }
    return {};
  }

  async function saveMerchant(param?: any) {
    const apiCall = param.id
      ? merchantBaseApi.updateMerchant
      : merchantBaseApi.createMerchant;
    const response = await apiCall({ ...param });
    if (response.code === 20000) {
      Message.success(param.id ? "更新成功" : "新增成功");
    }
  }

  async function deleteMerchant(param?: any) {
    const response = await merchantBaseApi.deleteMerchant(param);
    if (response.code === 20000) {
      Message.success("移除成功");
    }
  }

  async function getCategories(param?: any) {
    const response = await merchantBaseApi.getCategories(param);
    if (response.code === 20000) {
      return response.data.merchantCategories;
    }
    return [];
  }

  async function getCategory(param?: any) {
    const response = await merchantBaseApi.getCategory(param);
    if (response.code === 20000) {
      return response.data.merchantCategory;
    }
    return {};
  }

  async function saveCategory(param?: any) {
    const apiCall = param.id
      ? merchantBaseApi.updateCategory
      : merchantBaseApi.createCategory;
    const response = await apiCall({ ...param });
    if (response.code === 20000) {
      Message.success(param.id ? "更新成功" : "新增成功");
    }
  }

  async function deleteCategory(param?: any) {
    const response = await merchantBaseApi.deleteCategory(param);
    if (response.code === 20000) {
      Message.success("移除成功");
    }
  }

  return {
    getMerchants,
    getMerchant,
    saveMerchant,
    deleteMerchant,
    getCategories,
    getCategory,
    saveCategory,
    deleteCategory,
  };
});
